package edu.ynu.e_platform.service;

import cn.xuyanwu.spring.file.storage.FileInfo;
import cn.xuyanwu.spring.file.storage.FileStorageService;
import edu.ynu.e_platform.common.service.LogicService;
import edu.ynu.e_platform.dao.ItemDao;
import edu.ynu.e_platform.dao.ShopDao;
import edu.ynu.e_platform.dao.ShopItemDao;
import edu.ynu.e_platform.entities.Business;
import edu.ynu.e_platform.entities.Item;
import edu.ynu.e_platform.entities.Shop;
import edu.ynu.e_platform.entities.ShopItem;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author manwen Yang
 */
@Slf4j
@Service
public class ShopService extends LogicService<ShopDao, Shop, Long> {

    @Resource
    FileStorageService fileStorageService;

    @Resource
    ItemDao itemDao;

    @Resource
    ShopItemDao shopItemDao;

    public ShopService(@Autowired ShopDao lr) {
        super(lr);
    }

    /**
     * 门店修改店铺头像
     * */
    public String updateImg(Long shopId, MultipartFile file){
        FileInfo upload = fileStorageService.of(file)
                .upload();
        Integer res = getDAO().updateImgById(upload.getUrl(), shopId);
        if(res==0) {
            fileStorageService.delete(upload.getUrl());
            return "更新失败";
        }
        return "更新成功";
    }

    /**
     * 商店添加商品(默认不上架)
     * */
    public ShopItem addItem(Long shopId, Long itemId){

        try {
            Shop shop = GET(shopId);
            Item item = itemDao.getReferenceById(itemId);

            ShopItem shopItem = shop.addItem(item);
            shopItem = shopItemDao.save(shopItem);

            return shopItem;
        } catch (Exception e) {
            log.warn(e.toString());
        }

        return null;
    }

    /**
     * 商店上架商品
     * */
    public ShopItem putAwayItem(Long shopId, Long itemId){

        try {
            Shop shop = GET(shopId);
            Item item = itemDao.getReferenceById(itemId);

            ShopItem shopItem = shop.putAwayItem(item);

            shopItem = shopItemDao.save(shopItem);

            return shopItem;
        } catch (Exception e) {
            log.warn(e.toString());
        }

        return null;
    }

    /**
     * 商店下架商品
     * */
    public ShopItem removeItem(Long shopId, Long itemId){

        try {
            Shop shop = GET(shopId);
            Item item = itemDao.getReferenceById(itemId);

            ShopItem shopItem = shop.removeItem(item);

            shopItem = shopItemDao.save(shopItem);

            return shopItem;
        } catch (Exception e) {
            log.warn(e.toString());
        }

        return null;
    }

    public Page<ShopItem> PageQueryByItemStatus(Integer pageStart, Integer pageSize,Long shopId, Integer itemStatus){

        Shop shop = getDAO().getReferenceById(shopId);

        Pageable pageable = PageRequest.of(pageStart, pageSize, Sort.by("createTime").descending());

        return shopItemDao.pageFindShopItemByShop(shop, itemStatus,pageable);
    }

}
